﻿using Esri.ArcGISRuntime.Mapping;
using System;
using System.Collections.Generic;
using Esri.ArcGISRuntime.Geometry;
using System.Windows;
using Geometry = Esri.ArcGISRuntime.Geometry.Geometry;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Symbology;
using Color = System.Drawing.Color;

namespace classTest
{
    /// <summary>
    /// BufferWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BufferWindow : Window
    {
        private MainWindow mainWindow { get; set; }
        public BufferWindow(MainWindow mainWindow1)
        {
            InitializeComponent();
            mainWindow = mainWindow1;

            //初始化数据
            Initialize();
           
        }
        private void Initialize()
        {
            List<string> layerName = new List<string>();
            LayerCollection pLayers = mainWindow.MyMapView.Map.OperationalLayers;
            //for循环地图业务图层的所有图层名称与参数进行比对，一旦匹配到相等即返回对应图层
            for (int i = pLayers.Count - 1; i >= 0; i--)
            {
                layerName.Add(pLayers[i].Name);
            }
            comboBox.ItemsSource = layerName;
            comboBox.SelectedIndex = 0;
        }

        private async void Confirm_Click(object sender, RoutedEventArgs e)
        {
            FeatureLayer layer =  mainWindow.MyMapView.Map.OperationalLayers[comboBox.Items.Count-1- comboBox.SelectedIndex] as FeatureLayer;
            FeatureLayer layerClone = layer.Clone() as FeatureLayer;
            QueryParameters pQueryP = new QueryParameters();
            //SelectFeaturesAsync()执行查询,返回图层一维数组，并高亮显示。for循环遍历所有图层，将图层范围联合，并缩放至此范围
            FeatureQueryResult pQueryResult = await layer.FeatureTable.QueryFeaturesAsync(pQueryP);
            // Create the schema for a polygon table (one text field to contain a name attribute)
            List<Field> polyFields = new List<Field>();
            Field areaField = new Field(FieldType.Text, "AreaName", "Area Name", 50);
            polyFields.Add(areaField);

            // Instantiate FeatureCollectionTables with schema and geometry type
            FeatureCollectionTable polysTable = new FeatureCollectionTable(polyFields, GeometryType.Polygon, new SpatialReference(2383));

            foreach (Feature i in pQueryResult)
            {
                Geometry result = GeometryEngine.Buffer(i.Geometry, Convert.ToDouble(textBox.Text));
                // Create a new polygon feature, provide geometry and attribute values
                Feature polyFeature = polysTable.CreateFeature();
                polyFeature.SetAttributeValue(areaField, "Restricted area");
                polyFeature.Geometry = result;

                // Add the new features to the appropriate feature collection table 
                await polysTable.AddFeatureAsync(polyFeature);
            }

            // Set rendering for each table
            polysTable.Renderer = CreateRenderer(GeometryType.Polygon);

            // Create a feature collection and add the feature collection tables
            FeatureCollection featuresCollection = new FeatureCollection();
            featuresCollection.Tables.Add(polysTable);

            // Create a FeatureCollectionLayer 
            FeatureCollectionLayer collectionLayer = new FeatureCollectionLayer(featuresCollection);
            collectionLayer.Name = $"{layer.Name}_Buffer";

            // Add the layer to the Map's Operational Layers collection
            mainWindow.MyMapView.Map.OperationalLayers.Add(collectionLayer);

            mainWindow.AddLayerNameToList();
            Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private Renderer CreateRenderer(GeometryType rendererType)
        {
            // Return a simple renderer to match the geometry type provided
            Symbol sym = null;

            switch (rendererType)
            {
                case GeometryType.Point:
                case GeometryType.Multipoint:
                    // Create a marker symbol
                    sym = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Triangle, Color.Red, 18);
                    break;
                case GeometryType.Polyline:
                    // Create a line symbol
                    sym = new SimpleLineSymbol(SimpleLineSymbolStyle.Dash, Color.Green, 3);
                    break;
                case GeometryType.Polygon:
                    // Create a fill symbol
                    SimpleLineSymbol lineSym = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.DarkBlue, 2);
                    sym = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.YellowGreen, lineSym);
                    break;
                default:
                    break;
            }
            // Return a new renderer that uses the symbol created above
            return new SimpleRenderer(sym);
        }

    }
}
